from State import State
from os.path import isfile
from config import get_config


if isfile("./config.py"):
    from config import get_config

others_datas = get_config()["others"]
pv_datas = get_config()["PV"]


class Mode_CloseLoop(State):
    def handle(self, fsm):
        if (
            abs(self.pf.create_pv(pv_datas["幅度Error"]).get())
            <= others_datas["幅度Error范围"]
        ):
            if self.pf.create_pv(pv_datas["幅度闭环状态"]).get() == 0:
                self.pf.create_pv(pv_datas["幅度闭环开关"]).put(0)
                self.pf.create_pv(pv_datas["幅度闭环开关"]).put(1)
                return fsm.mode_openloop
            else:
                print("幅度闭环完成")
                return self
        else:
            return fsm.mode_openloop
